@using Grand.Business.Core.Interfaces.Catalog.Products
@using Grand.Business.Core.Interfaces.Checkout.Orders
@using Grand.Business.Core.Interfaces.Common.Directory
@using Grand.Business.Core.Interfaces.Common.Localization
@model IList<Grand.Domain.Shipping.Shipment>
@inject IOrderService _orderService
@inject IProductService _productService
@inject ICountryService _countryService
@inject IWorkContextAccessor _workContextAccessor
@inject ITranslationService _translationService

@{
    var companyName = _workContextAccessor.WorkContext.CurrentStore.CompanyName;
    var limitbyVendor = _workContextAccessor.WorkContext.CurrentVendor != null;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>

        .summary {
                    padding-top:20px;
                }
                table.CodeTable {
                              width: 100%;  
                      }
                      .CodeTable .line {border-bottom:1px solid #EEE; border-top: 1px solid #EEE; border-left: 1px solid #FFFFFF; border-right: 1px solid #FFFFFF;}
                      table.CodeTable th,
                      table.CodeTable td {
                        /* width: 100%; */
                        /*border: 0px solid black;*/
                        border: none;
                        border-collapse: collapse;
                      }
          
          .Header {
            background-color: grey;
            color: white;
            padding: 5px;
          }
    
          table.Table {
            width: 100%;
          }
          table.Table,
          table.Table th,
          table.Table td {
            white-space: nowrap;
            text-align: left;
            /* border: 1px solid black; */
            border-collapse: collapse;
          }
          table.Table th,
          table.Table td {
            padding-top: 12px;
            padding-bottom: 3px;
            padding-left: 8px;
            padding-right: 8px;
            text-align: right;
          }
          table.Table th {
            padding-top: 5px;
            padding-bottom: 5px;
            border-bottom: 1px solid black; ;
          }

          table.Table tfoot {
            padding-top: 4px;
            border-top: 1px solid black;
            border-bottom: 1px solid black;
          }
          table.Table tfoot tr,
          table.Table tfoot td {
            padding-top: 4px;
            padding-bottom: 4px;
          }
    
        </style>
</head>
<body>
<main style="padding:10pt; font-size:12px">
    @for (var i = 0; i < Model.Count; i++)
    {
        var order = await _orderService.GetOrderById(Model[i].OrderId);
        var shipment = Model[i];
        <div>
            <h4>@(string.Format(_translationService.GetResource("PDFPackagingSlip.Shipment", order.CustomerLanguageId), shipment.ShipmentNumber))</h4>
            <div>@companyName</div>
        </div>
        @if (order.ShippingAddress != null)
        {
            <div>
                <h6>@(_translationService.GetResource("PDFInvoice.ShippingInformation", order.CustomerLanguageId))</h6>
                @if (!string.IsNullOrEmpty(order.ShippingAddress.Company))
                {
                    @(string.Format(_translationService.GetResource("PDFPackagingSlip.Company", order.CustomerLanguageId), order.ShippingAddress.Company))<br/>
                }
                @(string.Format(_translationService.GetResource("PDFPackagingSlip.Name", order.CustomerLanguageId), order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName))<br/>
                @(string.Format(_translationService.GetResource("PDFPackagingSlip.Phone", order.CustomerLanguageId), order.ShippingAddress.PhoneNumber))<br/>
                @(string.Format(_translationService.GetResource("PDFPackagingSlip.Address", order.CustomerLanguageId), order.ShippingAddress.Address1))<br/>
                @(order.ShippingAddress.City + ", " + order.ShippingAddress.ZipPostalCode)<br/>
                @((await _countryService.GetCountryById(order.ShippingAddress.CountryId))?.Name)<br/>
            </div>
        }

        <div>
            <strong>@(string.Format(_translationService.GetResource("PDFPackagingSlip.ShippingMethod", order.CustomerLanguageId), order.ShippingMethod))</strong>
        </div>
        <div class="summary">
            @(_translationService.GetResource("PDFInvoice.Product(s)", order.CustomerLanguageId))
        </div>
        <table class="CodeTable">
            <tr>
                <td class="Header" colspan="2">@(_translationService.GetResource("PDFPackagingSlip.ProductName", order.CustomerLanguageId))</td>
                <td class="Header">@(_translationService.GetResource("PDFPackagingSlip.SKU", order.CustomerLanguageId))</td>
                <td class="Header">@(_translationService.GetResource("PDFPackagingSlip.QTY", order.CustomerLanguageId))</td>
            </tr>
            @foreach (var item in shipment.ShipmentItems)
            {
                var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == item.OrderItemId);
                if (orderItem != null && (orderItem.VendorId == _workContextAccessor.WorkContext.CurrentVendor?.Id || !limitbyVendor))
                {
                    <tr>
                        <td colspan="2">
                            @((await _productService.GetProductByIdIncludeArch(item.ProductId)).Name)
                            @if (!string.IsNullOrEmpty(orderItem.AttributeDescription))
                            {
                                <br/>
                                <small>@(Html.Raw(orderItem.AttributeDescription))</small>
                            }
                        </td>

                        <td>@((await _productService.GetProductByIdIncludeArch(item.ProductId)).Sku)</td>
                        <td>@item.Quantity</td>
                    </tr>
                }
            }
        </table>
    }
</main>
</body>
</html>